//添加移动 每次只能为一个对象添加移动事件
//如果要删除被绑定的元素，请调用unbindMove先移除它的移动事件
var _____hasBindContent = [];
$.extend($.fn,{
	bindMove:function(option){
		if(!option){ return;}
		var content = option.content;
		if(!content.length || content.length !== 1){ return ;}
		$(this).on({
			mousedown:function(e){
				var el = content;
				var x = el.offset().left;
				var y = el.offset().top;
				var smx = e.pageX - x;
				var smy = e.pageY - y;
				var initBottom = parseInt(el.css('bottom'));
				initBottom = isNaN(initBottom)?0:initBottom;
				$(document).on('mousemove.drag',function(e2){
					var lx = e2.pageX-smx;
					var ty = e2.pageY-smy;
					var maxX = $(document).width() - el.width();
					var maxY = $(document).height() - el.height();
					if(lx < 0){
						lx = 0;
					}else if(lx > maxX){
						lx = maxX;
					}
	
					if(ty < 0){
						ty = 0;
					}else if(ty > maxY){
						ty = maxY;
					}
	
					el.offset({left:lx,top:ty});
					el.css('bottom',initBottom-(ty - y)+"px");
				});
			},
			mouseup:function(){
				$(document).off("mousemove.drag");
			}
		});
		
		var event = $(window).resize(function(){
			if(content.offset().left < 0){
				content.offset({left:0});
			}
			if(content.offset().left + content.width() > $(document).width()){
				content.offset({left:$(document).width()-content.width()});
			}
			if(content.offset().top < 0){
				content.offset({top:0});
			}
			if(content.offset().top + content.height()> $(document).height()){
				console.log($(document).height() - content.height())
				content.offset({top:$(document).height() - content.height()});
			}
		});
		_____hasBindContent.push({content:content[0],event:event,root:$(this)[0]});
	},
	unbindMove:function(){
		if(_____hasBindContent && _____hasBindContent.length > 0){
			var self = $(this)[0]; console.log(self === _____hasBindContent[0].root)
			for(var i in _____hasBindContent) if(self === _____hasBindContent[i].root ) {_____hasBindContent[i].event.unbind();break;}
		}
	}
})